package com.mall.mallDemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.mallDemo.exception.ExceptionEnum;
import com.mall.mallDemo.exception.MyException;
import com.mall.mallDemo.mapper.ProductPictureMapper;
import com.mall.mallDemo.pojo.ProductPicture;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Lii
 * @create 2022-06-15 10:49
 */
@Service("productPictureService")
public class ProductPictureService extends ServiceImpl<ProductPictureMapper, ProductPicture> implements com.mall.mallDemo.service.ProductPictureService {

    @Autowired
    private ProductPictureMapper productPictureMapper;

    public List<ProductPicture> getProductPictureByProductId(String productId) {
        ProductPicture picture = new ProductPicture();
        picture.setProductId(Integer.parseInt(productId));

        QueryWrapper<ProductPicture> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id",productId);
        List<ProductPicture> list = null;
        try {
            list = productPictureMapper.selectList(queryWrapper);
            if (ArrayUtils.isEmpty(list.toArray())) {
                throw new MyException(ExceptionEnum.GET_PRODUCT_PICTURE_NOT_FOUND);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(ExceptionEnum.GET_PRODUCT_PICTURE_ERROR);
        }
        return list;
    }
}
